package com.github.edu.web.organization.controller;

import com.github.admin.edu.oauth.server.api.entity.TSysOrganization;
import com.github.edu.web.organization.service.OrganizationManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 组织机构管理
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-2
 */
@Controller
@RequestMapping("/uc/organization")
public class OrganizationManagerController {

    @Autowired
    private OrganizationManagerService service;

    @RequestMapping("/index")
    public String index(ModelMap map){
        service.index(map);
        return "index/organization/index";
    }

    @GetMapping("/add/entity/root")
    public String addRoot(ModelMap map,TSysOrganization organization){
        if(null==organization){
            organization=new TSysOrganization();
        }
        organization.setType(1);
        organization.setParentid(-1);
        map.put("button",true);
        map.put("entity",organization);
        map.put("msg","open");
        return "index/organization/entity";
    }


    @GetMapping("/open/entity")
    public String openEntity(ModelMap map,@RequestParam(name = "id",required = false,defaultValue = "-1")Integer id){
        service.openEntity(map,id);
        return "index/organization/open";
    }

    @GetMapping("/open/update")
    public String update(ModelMap map,@RequestParam(name = "id",required = false,defaultValue = "-1")Integer id){
        service.openEntity(map,id);
        map.put("button",true);
        map.put("msg","open");
        return "index/organization/entity";
    }


    /**
     * 打开添加子节点页面
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/open/child/entity")
    public String openChild(ModelMap map,@RequestParam("pid")Integer id){
        service.openChild(map,id);
        map.put("button",true);
        map.put("msg","open");
        return "index/organization/entity";
    }

    @PostMapping("/entity/save")
    public String save(TSysOrganization organization, ModelMap map){
        service.save(organization,map);
        return "index/organization/entity";
    }

    /**
     * 查询全部节点信息
     * @return
     */
    @GetMapping("/ajax/tree")
    @ResponseBody
    public String getOrganization(){
        return service.getAllTree();
    }


    /**
     * 删除当前节点
     * @param id
     * @return
     */
    @GetMapping("/ajax/delete")
    @ResponseBody
    public String deleteObject(@RequestParam("id")Integer id){
        return service.delete(id);
    }

}
